"""

@author: lingw
@email: gw.lin@hzgosun.com
@file: PmManageTest.py
@time: 2019-5-28 下午 4:44

这一行开始写关于本文件的说明与解释

"""

import unittest

from time import sleep

from selenium.webdriver.common.keys import Keys

from common.decorators import mkdir_classname,log4gs
from common.functions import save_screenshot
from common.settings import TAG
from common.vue.select import DropDownSelect, OperateSelect
from pages.pmManagePage import PmManagePage


@log4gs
@mkdir_classname(TAG)
class PmManageTest(unittest.TestCase):
    """标点账号管理功能测试"""

    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 + "开始标点账号管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = PmManagePage('/PopcManage/pmManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束标点账号管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info(">>>>>>开始执行用例:{} {}".format(self.id(), self.shortDescription()))

    def tearDown(self):
        self.log.info("<<<<<<结束执行用例:{} {}".format(self.id(), self.shortDescription()))


    @unittest.skip
    def test_1_turn_page(self):
        """翻页功能测试"""
        self.log.debug("当前共有：" + self.page.turnPage.total_page +"页数据" )
        self.log.debug("当前处于第：" + self.page.turnPage.current_page + "页数据")
        self.assertEqual(self.page.turnPage.current_page, '1')
        sleep(1)
        try:
            self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')

            sleep(1)
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(6)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '6')
        except Exception:
            self.log.error("当前模块最大为1页，无法进行翻页测试", exc_info=True)


    # @unittest.skip
    def test_2_add_pm_manage(self):
        """增加项目经理账号测试
        http://181.181.0.216:8786/tc/detail/74-1654/
        """

        self.page.table.add_btn.click()
        self.page.addPmManage.add_name.send_keys('测试项目经理名称')
        self.page.addPmManage.add_phone.send_keys('13777777777')
        sleep(1)
        DropDownSelect(self.page.addPmManage.add_state, '在职')
        sleep(1)

        self.page.addPmManage.confirm.click()
        sleep(1)

        self.page.searchTools.search_name.send_keys('测试项目经理名称')
        self.page.searchTools.search_btn.click()
        sleep(1)

        add_result = self.page.table.get_row('测试项目经理名称')

        try:
            self.assertTrue(add_result, msg='无法查询到新增加的项目经理')
            self.log.info('添加项目经理用例执行成功')
        except Exception:
            self.log.error('添加项目经理用例执行失败', exc_info=True)
            save_screenshot('添加项目经理用例执行失败截图')
        self.page.searchTools.search_name.clear()

    # @unittest.skip
    def test_3_search_pm_manage(self):
        """查询项目经理账号测试
        http://181.181.0.216:8786/tc/detail/74-1666/
        """

        self.page.searchTools.search_name.send_keys('测试项目经理名称')
        self.page.searchTools.search_btn.click()
        sleep(1)

        search_result = self.page.table.get_row('测试项目经理名称')

        try:
            self.assertTrue(search_result, msg='无法查询到新增加的项目经理名称')
            self.log.info('查询项目经理用例执行成功')
        except Exception:
            self.log.error('查询项目经理用例执行失败', exc_info=True)
            save_screenshot('查询项目经理用例执行失败截图')
        self.page.searchTools.search_name.clear()

    # @unittest.skip
    def test_4_modify_pm_manage(self):
        """修改项目经理账号测试"""

        self.page.searchTools.search_name.send_keys('测试项目经理名称')
        self.page.searchTools.search_btn.click()
        sleep(1)

        search_result = self.page.table.get_row('测试项目经理名称')
        OperateSelect(search_result, '修改')
        sleep(1)

        self.page.modifyPmManage.add_name.clear()
        self.page.modifyPmManage.add_name.send_keys('测试修改Pm名称')
        self.page.modifyPmManage.confirm.click()
        sleep(1)

        self.page.searchTools.search_name.clear()
        self.page.searchTools.search_name.send_keys('测试修改Pm名称')
        self.page.searchTools.search_btn.click()
        sleep(1)

        modify_result = self.page.table.get_row('测试修改Pm名称')

        try:
            self.assertTrue(modify_result, msg='无法查询到修改后的项目经理名称')
            self.log.info('修改项目经理用例执行成功')
        except Exception:
            self.log.error('修改项目经理用例执行失败', exc_info=True)
            save_screenshot('修改项目经理用例执行失败截图')
        self.page.searchTools.search_name.clear()

    # @unittest.skip
    def test_5_delete_pm_manage(self):
        """删除项目经理账号测试
        http://181.181.0.216:8786/tc/detail/74-1665/
        """

        self.page.searchTools.search_name.send_keys('测试修改Pm名称')
        self.page.searchTools.search_btn.click()
        sleep(1)

        search_result = self.page.table.get_row('测试修改Pm名称')
        OperateSelect(search_result, '删除')
        sleep(1)

        self.page.searchTools.search_btn.click()
        sleep(1)
        delete_result = self.page.table.get_row('测试修改Pm名称')

        try:
            self.assertFalse(delete_result, msg='查询到删除后的项目经理名称')
            self.log.info('删除项目经理用例执行成功')
        except Exception:
            self.log.error('删除项目经理用例执行失败', exc_info=True)
            save_screenshot('删除项目经理用例执行失败截图')



if __name__ == '__main__':
    unittest.main()

